<template>
  <a-modal
    :title="title"
    :width="460"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    okText="保存"
    wrapClassName="ant-modal-cust-warp">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="货架名称" clearable>
          <a-input placeholder="请输入货架名称" v-decorator="['data_name', validatorRules.data_name]" v-model="data_name"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="货架编号" clearable>
          <a-input placeholder="请输入货架编号" v-decorator="['data_code', validatorRules.data_code]" v-model="data_code"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排列序号"
          hasFeedback >
          <a-input placeholder="请输入排列序号" v-decorator="['data_numbers', validatorRules.data_numbers]" v-model="data_numbers"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="货架层数" clearable>
          <a-input placeholder="请输入货架层数" v-decorator="['data_ceng', validatorRules.data_ceng]" v-model="data_ceng" disabled="disabled" v-show="this.title=='编辑货架'"/>
          <a-input placeholder="请输入货架层数" v-decorator="['data_ceng', validatorRules.data_ceng]" v-model="data_ceng" v-show="this.title=='新增货架'"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="货架列数" clearable>
          <a-input placeholder="请输入货架列数" v-decorator="['data_column', validatorRules.data_column]" v-model="data_column" disabled="disabled" v-show="this.title=='编辑货架'"/>
          <a-input placeholder="请输入货架列数" v-decorator="['data_column', validatorRules.data_column]" v-model="data_column" v-show="this.title=='新增货架'"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注"
          style="margin-top: 0px;" >
          <a-textarea placeholder="请输入备注" v-model="data_remark" rows="3"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction, postAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "JeecgDemoModal",
    components: {
    },
    data () {
      return {
        data_name:'',//仓库名称
        data_code:'',//仓库编码
        data_numbers:'0',
        data_ceng:'',
        data_column:'',
        data_remark:'',
        dataDetail:[],//数据对象
        title:"操作",
        visible: false,
        model: {},
        validatorRules:{//验证规则
          data_name:{
            rules: [
              { required: true, message: '请输入货架名称' },
              { min: 2, max: 30, message: '仓库名称长度在 2 到 30 个字符', trigger: 'blur' }
            ]},
          data_code:{
            rules: [
              { required: true, message: '请输入货架编码'}
            ]},
          data_numbers:{
            rules: [
              {
                required: true,
                pattern:/^[0-9][0-9]*(\.[0-9]{1,2})?$/,
                message: '请输入货架的正确排序'
              }
            ]},
          data_ceng:{
            rules: [
              {
                required: true,
                pattern:/^[1-9]\d*$/,
                message: '请输入正整数的货架层数'
              }
            ]},
          data_column:{
            rules: [
              {
                required: true,
                pattern:/^[1-9]\d*$/,
                message: '请输入正整数的货架列数'
              }
            ]},
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        url: {
          query_detail:"/location/configuration/query_ShelvesById",
          add: "/location/configuration/add_Shelves",
          edit: "/location/configuration/update_Shelves"
        },
      }
    },
    methods: {
      edit (record) {
        this.data_remark='',
          this.form.setFieldsValue({
            data_name:'',
            data_code:'',
            data_numbers:'',
            data_ceng:'',
            data_column:''
          })
        this.model = Object.assign({}, record);
        this.visible = true;//开启弹框
        if(record=="0"){
          this.title="新增货架"
        }else {
          this.title="编辑货架"
          this.show_datainfodetail(this.model.id);
        }
      },
      show_datainfodetail(id) {//查询详情
        let params = {id:id};
        getAction(this.url.query_detail,params).then((res)=>{
          if(res.success){
            this.form.setFieldsValue({
              data_name:res.result.shelvesName,
              data_code:res.result.shelvesCode,
              data_numbers:res.result.numbers,
              data_ceng:res.result.layer,
              data_column:res.result.columns
            })
            this.data_name = res.result.shelvesName;
            this.data_code = res.result.shelvesCode;
            this.data_numbers = res.result.numbers;
            this.data_ceng = res.result.layer;
            this.data_column = res.result.columns;
            this.data_remark=res.result.remark;
          }else {
            this.$message.error("查询失败")
          }
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            var reg = /(^[1-9]\d*$)/;
            if (!reg.test(this.data_ceng)) {
               this.$message.warning("货架层数需要输入正整数")
                return
            }
            if (!reg.test(this.data_column)) {
              this.$message.warning("货架列数需要输入正整数")
              return
            }
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            let params = '';
            if(this.model.id!=null && this.model.id!=''){
              httpurl+=this.url.edit;
              method = 'put';
              params = {shelvesName:this.data_name,
                numbers:this.data_numbers,
                shelvesCode:this.data_code,
                layer:this.data_ceng,
                columns:this.data_column,
                remark:this.data_remark,
                id:this.model.id}
            }else{
              httpurl+=this.url.add;
              method = 'post';
              params = {shelvesName:this.data_name,
                numbers:this.data_numbers,
                shelvesCode:this.data_code,
                layer:this.data_ceng,
                columns:this.data_column,
                reservoirId:this.$route.query.kuqu_id,
                remark:this.data_remark}
            }
            httpAction(httpurl,params,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.confirmLoading = false;
                that.$emit('ok');
                that.close();
              }else{
                that.$message.warning(res.message);
                that.confirmLoading = false;
              }
            }).finally(() => {
              // that.confirmLoading = false;
              // that.$emit('ok');
              // that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>